package nix.web.action;

import nix.domain.User;
import nix.service.UserService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User> {

	private UserService userService;
	private User user = new User();

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String login() throws Exception {

		User u = userService.getUserByCodePassword(user);
		ActionContext.getContext().getSession().put("session_user", u);
		return "toHome";
	}
	
	public String regist() throws Exception {

		try {
			userService.saveUser(user);
		} catch (Exception e) {
			ActionContext.getContext().put("error", e.getMessage());
			return "regist";
		}	
		return "toLogin";
	}	

	@Override
	public User getModel() {
		return this.user;
	}
}
